// API related types

import { User, LoginCredentials, RegisterData } from './auth';
import { VideoFile, ProcessTask, ProcessOptions } from './video';
import { PaginationParams, PaginationResponse } from './common';

// API Base Types
export interface TauriApiResponse<T = unknown> {
  success: boolean;
  data?: T;
  error?: string;
  message?: string;
}

export interface TauriError {
  code: string;
  message: string;
  details?: any;
}

// Auth API Types
export interface AuthApi {
  login: (credentials: LoginCredentials) => Promise<TauriApiResponse<User>>;
  register: (data: RegisterData) => Promise<TauriApiResponse<User>>;
  logout: () => Promise<TauriApiResponse<void>>;
  getCurrentUser: () => Promise<TauriApiResponse<User>>;
  updateProfile: (data: Partial<User>) => Promise<TauriApiResponse<User>>;
  changePassword: (oldPassword: string, newPassword: string) => Promise<TauriApiResponse<void>>;
  deleteAccount: () => Promise<TauriApiResponse<void>>;
}

// Video API Types
export interface VideoApi {
  selectFiles: () => Promise<TauriApiResponse<string[]>>;
  selectFolder: () => Promise<TauriApiResponse<string>>;
  getVideoInfo: (path: string) => Promise<TauriApiResponse<VideoFile>>;
  processVideo: (
    inputPath: string,
    outputPath: string,
    options: ProcessOptions
  ) => Promise<TauriApiResponse<ProcessTask>>;
  batchProcess: (
    inputPaths: string[],
    outputPath: string,
    options: ProcessOptions
  ) => Promise<TauriApiResponse<ProcessTask[]>>;
  getProcessStatus: (taskId: string) => Promise<TauriApiResponse<ProcessTask>>;
  cancelProcess: (taskId: string) => Promise<TauriApiResponse<void>>;
  pauseProcess: (taskId: string) => Promise<TauriApiResponse<void>>;
  resumeProcess: (taskId: string) => Promise<TauriApiResponse<void>>;
  generateThumbnail: (
    videoPath: string,
    outputPath: string,
    timestamp: number
  ) => Promise<TauriApiResponse<string>>;
  extractAudio: (
    videoPath: string,
    outputPath: string,
    format: string
  ) => Promise<TauriApiResponse<string>>;
  getVideoFormats: () => Promise<TauriApiResponse<string[]>>;
  getAudioFormats: () => Promise<TauriApiResponse<string[]>>;
}

// File System API Types
export interface FileSystemApi {
  exists: (path: string) => Promise<TauriApiResponse<boolean>>;
  createDir: (path: string) => Promise<TauriApiResponse<void>>;
  removeFile: (path: string) => Promise<TauriApiResponse<void>>;
  removeDir: (path: string) => Promise<TauriApiResponse<void>>;
  copyFile: (src: string, dest: string) => Promise<TauriApiResponse<void>>;
  moveFile: (src: string, dest: string) => Promise<TauriApiResponse<void>>;
  readDir: (path: string) => Promise<TauriApiResponse<DirEntry[]>>;
  getFileSize: (path: string) => Promise<TauriApiResponse<number>>;
  getFileInfo: (path: string) => Promise<TauriApiResponse<FileInfo>>;
  openPath: (path: string) => Promise<TauriApiResponse<void>>;
  openInFileManager: (path: string) => Promise<TauriApiResponse<void>>;
}

export interface DirEntry {
  name: string;
  path: string;
  isDir: boolean;
  isFile: boolean;
  size?: number;
  modifiedAt?: Date;
}

export interface FileInfo {
  name: string;
  path: string;
  size: number;
  isDir: boolean;
  isFile: boolean;
  extension: string;
  mimeType?: string;
  createdAt: Date;
  modifiedAt: Date;
  permissions: FilePermissions;
}

export interface FilePermissions {
  readonly: boolean;
  writable: boolean;
  executable: boolean;
}

// System API Types
export interface SystemApi {
  getSystemInfo: () => Promise<TauriApiResponse<SystemInfo>>;
  getAppVersion: () => Promise<TauriApiResponse<string>>;
  checkForUpdates: () => Promise<TauriApiResponse<UpdateInfo>>;
  installUpdate: () => Promise<TauriApiResponse<void>>;
  restartApp: () => Promise<TauriApiResponse<void>>;
  getStorageInfo: () => Promise<TauriApiResponse<StorageInfo>>;
  checkFFmpegInstallation: () => Promise<TauriApiResponse<FFmpegInfo>>;
  getDefaultPaths: () => Promise<TauriApiResponse<DefaultPaths>>;
  showNotification: (title: string, body: string) => Promise<TauriApiResponse<void>>;
}

export interface SystemInfo {
  platform: string;
  arch: string;
  osVersion: string;
  totalMemory: number;
  availableMemory: number;
  cpuCount: number;
  cpuModel: string;
  gpuInfo?: string[];
}

export interface UpdateInfo {
  available: boolean;
  version?: string;
  notes?: string;
  downloadUrl?: string;
  publishedAt?: Date;
}

export interface StorageInfo {
  total: number;
  available: number;
  used: number;
  tempDir: string;
  appDataDir: string;
  documentsDir: string;
  downloadsDir: string;
}

export interface FFmpegInfo {
  installed: boolean;
  version?: string;
  path?: string;
  supportedFormats: string[];
  supportedCodecs: string[];
}

export interface DefaultPaths {
  documents: string;
  downloads: string;
  pictures: string;
  videos: string;
  desktop: string;
  appData: string;
  temp: string;
}

// Settings API Types
export interface SettingsApi {
  get: <T = any>(key: string) => Promise<TauriApiResponse<T>>;
  set: (key: string, value: any) => Promise<TauriApiResponse<void>>;
  remove: (key: string) => Promise<TauriApiResponse<void>>;
  clear: () => Promise<TauriApiResponse<void>>;
  getAll: () => Promise<TauriApiResponse<Record<string, any>>>;
  export: (path: string) => Promise<TauriApiResponse<void>>;
  import: (path: string) => Promise<TauriApiResponse<void>>;
}

// Database API Types (for local data)
export interface DatabaseApi {
  getVideoHistory: (
    params?: PaginationParams
  ) => Promise<TauriApiResponse<PaginationResponse<ProcessTask>>>;
  getVideoStats: () => Promise<TauriApiResponse<VideoStats>>;
  saveVideoFile: (video: VideoFile) => Promise<TauriApiResponse<VideoFile>>;
  deleteVideoFile: (id: string) => Promise<TauriApiResponse<void>>;
  searchVideos: (
    query: string,
    params?: PaginationParams
  ) => Promise<TauriApiResponse<PaginationResponse<VideoFile>>>;
  getRecentVideos: (limit?: number) => Promise<TauriApiResponse<VideoFile[]>>;
}

export interface ProcessResult {
  taskId: string;
  inputPath: string;
  outputPath: string;
  status: 'completed' | 'failed' | 'cancelled';
  duration: number;
  sizeReduction?: number;
  error?: string;
}

export interface VideoStats {
  totalVideosProcessed: number;
  totalProcessingTime: number;
  totalSpaceSaved: number;
  averageCompressionRatio: number;
  mostUsedFormat: string;
  formatUsage: Record<string, number>;
  processingTimeByDay: Array<{ date: string; time: number }>;
  compressionRatioByDay: Array<{ date: string; ratio: number }>;
}

// Event Types for real-time communication
export interface EventPayloads {
  'task-progress': {
    taskId: string;
    progress: number;
    currentFrame: number;
    totalFrames: number;
    fps: number;
    timeRemaining: number;
  };
  'task-completed': {
    taskId: string;
    result: ProcessResult;
  };
  'task-failed': {
    taskId: string;
    error: TauriError;
  };
  'task-canceled': {
    taskId: string;
  };
  'system-notification': {
    title: string;
    message: string;
    type: 'info' | 'success' | 'warning' | 'error';
  };
}

export type EventName = keyof EventPayloads;
export type EventCallback<T extends EventName> = (payload: EventPayloads[T]) => void;
